package com.loie.datacenter.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.loie.datacenter.mapper.RoleListMapper;
import com.loie.datacenter.model.bo.ParamBO;
import com.loie.datacenter.model.bo.RoleListBO;
import com.loie.datacenter.model.bo.RoleMenuBO;
import com.loie.datacenter.service.RoleListService;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @author zhl
 * @create 2021/9/7 13:50
 */
@Transactional(readOnly = false)
@Service("RoleListService")
@AllArgsConstructor
public class RoleListServiceImpl implements RoleListService {

    final RoleListMapper roleListMapper;

    @Override
    public List<RoleListBO> getRoleList(ParamBO bo) {
        List<RoleListBO> roleListBOList = roleListMapper.getRoleList(bo);
        for (RoleListBO roleListBO : roleListBOList) {
            List<String> menuList = roleListMapper.getMenuListByRole(roleListBO.getId());
            roleListBO.setMenuId(StringUtils.join(menuList, ","));
        }
        return roleListBOList;
    }

    @Override
    public
    RoleListBO getRoleById(RoleListBO bo) {
        return roleListMapper.selectById(bo.getId());
    }

    @Override
    public void updateRoleList(RoleListBO bo) {
        if (StringUtils.isNotBlank(bo.getId())) {
            roleListMapper.updateById(bo);
        } else {
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date date = new Date();
            bo.setCreatedDate(date);
            bo.setId(StringUtils.upperCase(UUID.randomUUID().toString().replace("-", "")));
            roleListMapper.insert(bo);
        }

        String menuId = bo.getMenuId();
        String[] list = menuId.split(",");
        roleListMapper.deleteRoleMenuByRoleId(bo.getId());
        for (String id : list) {
            RoleMenuBO roleMenuBO = new RoleMenuBO();
            roleMenuBO.setRoleId(bo.getId());
            roleMenuBO.setId(StringUtils.upperCase(UUID.randomUUID().toString().replace("-", "")));
            roleMenuBO.setMenuId(id);
            roleListMapper.insertRoleMenu(roleMenuBO);
        }

    }

    @Override
    public void deleteRoleList(RoleListBO bo) {
        roleListMapper.deleteById(bo.getId());
        roleListMapper.deleteRoleMenuByRoleId(bo.getId());
    }

    @Override
    public boolean isExist(RoleListBO bo) {
        boolean flag = false;
        long count = roleListMapper.isExist(bo);
        if (count > 0) {
            flag = true;
        }
        return flag;
    }

    @Override
    public int getRoleListCount() {
        QueryWrapper<RoleListBO> queryWrapper = new QueryWrapper<>();
        return Math.toIntExact(roleListMapper.selectCount(queryWrapper));
    }

    @Override
    public int getUserRoleCount(String roleId) {
        return roleListMapper.getUserRoleCount(roleId);
    }
}
